package org.sprogram.utils
{
	import flash.display.FrameLabel;
	import flash.display.MovieClip;

	public class MovieClipUtil
	{
		public function MovieClipUtil()
		{
		}
		
		/**
		 * 获取帧标签所在的帧的帧号
		 */		
		public static function getFrame(movieClip:MovieClip, label:String):int
		{
			for (var i:int = 0; i < movieClip.currentLabels.length; i++)
			{
				var frameLabel:FrameLabel = movieClip.currentLabels[i];
				
				if (frameLabel.name == (label as String))
				{
					return frameLabel.frame;
					break;
				}
			}
			return -1;
		}
		
		/**
		 * 为指定帧添加帧事件
		 */
		public static function handleMovieClip(movieClip:MovieClip, callBack:Function = null, pointer:Object = null):void
		{
			if (movieClip != null)
			{
				var frame:int = movieClip.totalFrames;
				
				if ((pointer is int) && (pointer as int) > 0 && (pointer as int) < movieClip.totalFrames)
				{
					frame = pointer as int;
				}
				else if (pointer is String)
				{
					frame = getFrame(movieClip, pointer.toString());
					if(frame == -1)
					{
						frame = movieClip.totalFrames;
					}
				}
				else
				{
					frame = movieClip.totalFrames;
				}
				
				movieClip.addFrameScript(frame - 1, callBack);
			}
		}
	}
}